﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-11>
//创建作者：<郭登杰>
//功能说明：来函流程公共控件-已审批过的步骤意见
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Web.UI;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHLCYIJIANS : UserControl, IUCLHOperate
    {
        #region 变量

        private readonly SpHelper _sphelper = new SpHelper();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) InitControl();
        }

        #endregion

        #region 属性

        /// <summary>
        ///     来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        ///     来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }

        /// <summary>
        ///     主任意见
        /// </summary>
        public string ZhuRenYJ
        {
            get { return txtZhuRenYJ.Text; }
            set { txtZhuRenYJ.Text = value; }
        }

        /// <summary>
        ///     节水科意见
        /// </summary>
        public string JieShuiKYJ
        {
            get { return txtJieShuiKYJ.Text; }
            set { txtJieShuiKYJ.Text = value; }
        }

        /// <summary>
        ///     计财科意见
        /// </summary>
        public string JiCaiKYJ
        {
            get { return txtJiCaiKYJ.Text; }
            set { txtJiCaiKYJ.Text = value; }
        }

        /// <summary>
        ///     处长意见
        /// </summary>
        public string CuZhangYJ
        {
            get { return txtCuZhangYJ.Text; }
            set { txtCuZhangYJ.Text = value; }
        }

        public bool FSXG
        {
            set { ViewState["SFXG"] = value; }
            get { return ViewState["SFXG"] == null ? false : Convert.ToBoolean(ViewState["SFXG"]); }
        }

        #endregion

        #region 方法

        /// <summary>
        ///     配置控件
        /// </summary>
        public void InitControl()
        {
            txtCuZhangYJ.ClientEnabled = false;
            txtJiCaiKYJ.ClientEnabled = false;
            txtJieShuiKYJ.ClientEnabled = false;
            txtZhuRenYJ.ClientEnabled = false;
            hr.Visible = false;
            hrJHBG.Visible = false;
            trChuZhang.Visible = false;
            trJiCaiKe.Visible = false;
            trJieShuiKe.Visible = false;
            trZhuRen.Visible = false;
            tabJHBG.Visible = false;
            lblJHBGSQL.Visible = false;
            txtJHBGSQL.Visible = false;
            txtJHBGSQL.ClientEnabled = false;
            lblJHBGYWY.Visible = false;
            txtJHBGYWY.Visible = false;
            txtJHBGYWY.ClientEnabled = false;
            lblJHBGZR.Visible = false;
            txtJHBGZR.Visible = false;
            txtJHBGZR.ClientEnabled = false;
            lblJHBGJSK.Visible = false;
            txtJHBGJSK.Visible = false;
            txtJHBGJSK.ClientEnabled = false;
            lblJHBGJCK.Visible = false;
            txtJHBGJCK.Visible = false;
            txtJHBGJCK.ClientEnabled = false;
            txtCuZhangYJ.Text = "";
            txtJiCaiKYJ.Text = "";
            txtJieShuiKYJ.Text = "";
            txtZhuRenYJ.Text = "";
            txtJHBGYWY.Text = "";
            txtJHBGZR.Text = "";
            txtJHBGJSK.Text = "";
            txtJHBGJCK.Text = "";
            txtJHBGSQL.Text = "";
            txtJHBGSQL.Attributes.Add("ondblclick",
                "doubleClickCopy(" + txtJHBGSQL.ClientInstanceName + ",txtJHBGSPL)");
            txtJHBGYWY.Attributes.Add("ondblclick",
                "doubleClickCopy(" + txtJHBGYWY.ClientInstanceName + ",txtJHBGSPL)");
            txtJHBGZR.Attributes.Add("ondblclick", "doubleClickCopy(" + txtJHBGZR.ClientInstanceName + ",txtJHBGSPL)");
            txtJHBGJSK.Attributes.Add("ondblclick",
                "doubleClickCopy(" + txtJHBGJSK.ClientInstanceName + ",txtJHBGSPL)");
            txtJHBGJCK.Attributes.Add("ondblclick",
                "doubleClickCopy(" + txtJHBGJCK.ClientInstanceName + ",txtJHBGSPL)");
        }

        /// <summary>
        ///     提交验证
        /// </summary>
        public ValidateRS Validate()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     清除数据
        /// </summary>
        public void ClearInfo()
        {
            InitControl();
        }

        /// <summary>
        ///     控件显隐
        /// </summary>
        private void SetDisPlay(YS_LH_LAIHANXX laihanxx)
        {
            InitControl();
            if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销 ||
                laihanxx.I_LAIHANLX == (int) eLaiHanLX.月度) tabJHBG.Visible = true;
            var hasDoSQL = (int) laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.登记;
            var hasDoYWY = laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.业务员;
            var hasDoZR = laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.主任;
            var hasDoJSK = laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.节水科;
            var hasDoJCK = laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.计财科;
            var hasDoCZ = laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.处长;
            //计划变更量
            lblJHBGSQL.Visible = hasDoSQL;
            txtJHBGSQL.Visible = hasDoSQL;
            lblJHBGYWY.Visible = hasDoYWY;
            txtJHBGYWY.Visible = hasDoYWY;
            lblJHBGZR.Visible = hasDoZR;
            txtJHBGZR.Visible = hasDoZR;
            lblJHBGJSK.Visible = hasDoJSK;
            txtJHBGJSK.Visible = hasDoJSK;
            lblJHBGJCK.Visible = hasDoJCK;
            txtJHBGJCK.Visible = hasDoJCK;
            //通用意见
            trZhuRen.Visible = hasDoZR;
            trJieShuiKe.Visible = hasDoJSK;
            trJiCaiKe.Visible = hasDoJCK;
            trChuZhang.Visible = hasDoCZ;
            if (BusinessTool.WType != 0)
            {
                lblZR.Text = "取水科长意见";
                trJieShuiKe.Visible = false;
            }
            else
            {
                lblZR.Text = "主任意见";
            }

            if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销 ||
                laihanxx.I_LAIHANLX == (int) eLaiHanLX.月度)
            {
                if (hasDoSQL) hr.Visible = true;
                if (hasDoZR || hasDoJSK || hasDoJCK || hasDoCZ) hrJHBG.Visible = true;
            }
            else
            {
                if (hasDoZR || hasDoJSK || hasDoJCK || hasDoCZ) hr.Visible = true;
            }

            if (FSXG) FSXGEnabled(true);
        }

        /// <summary>
        ///     根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //if (laihanxx.ID == 0)
            //{
            //    this.LaiHanLC = eLaiHanLCBH.未登记;
            //    return;
            //}
            //else
            //{
            //    this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
            //}
            SetDisPlay(laihanxx);
            var drc = _sphelper.P_YS_LHGETSHENPIYJS((int) laihanxx.I_LAIHANLX, laihanxx.S_LAIHANBH).Tables[0].Rows;
            if (drc.Count > 0)
            {
                if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.月度)
                {
                    txtZhuRenYJ.Text = Convert.ToString(drc[0]["S_ZHUREN"]);
                    txtJieShuiKYJ.Text = Convert.ToString(drc[0]["S_JIESHUIKZ"]);
                    txtJiCaiKYJ.Text = Convert.ToString(drc[0]["S_JICAIKZ"]);
                    txtJHBGSQL.Text = Convert.ToString(drc[0]["I_SHENQINGL"]);
                    txtJHBGYWY.Text = drc[0]["I_YEWUYJY"] == DBNull.Value ? "" : Convert.ToString(drc[0]["I_YEWUYJY"]);
                    txtJHBGZR.Text = drc[0]["I_ZHUREN"] == DBNull.Value ? "" : Convert.ToString(drc[0]["I_ZHUREN"]);
                    txtJHBGJSK.Text = drc[0]["I_JIESHUIKZ"] == DBNull.Value
                        ? ""
                        : Convert.ToString(drc[0]["I_JIESHUIKZ"]);
                    txtJHBGJCK.Text = drc[0]["I_JICAIKZ"] == DBNull.Value ? "" : Convert.ToString(drc[0]["I_JICAIKZ"]);
                    txtJHBGCZ.Text = drc[0]["I_CHUZHANG"] == DBNull.Value ? "" : Convert.ToString(drc[0]["I_CHUZHANG"]);
                    return;
                }

                if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.分户)
                {
                    txtZhuRenYJ.Text = drc[0]["S_ZHUREN"].ToString();
                    txtJieShuiKYJ.Text = drc[0]["S_JieSHUI"].ToString();
                    return;
                }

                var indexs = new List<int>();
                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.登记)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.登记);
                    if (indexs.Count > 0)
                        if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 ||
                            laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销)
                            txtJHBGSQL.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                }

                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.业务员)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.业务员);
                    if (indexs.Count > 0)
                        if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 ||
                            laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销)
                            txtJHBGYWY.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                }

                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.主任)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.主任);
                    if (indexs.Count > 0)
                    {
                        txtZhuRenYJ.Text = Convert.ToString(drc[indexs[0]]["S_SHENPIYJ"]);
                        if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销
                        ) txtJHBGZR.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                    }
                }

                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.节水科)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.节水科);
                    if (indexs.Count > 0)
                    {
                        txtJieShuiKYJ.Text = Convert.ToString(drc[indexs[0]]["S_SHENPIYJ"]);
                        if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销
                        ) txtJHBGJSK.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                    }
                }

                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.计财科)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.计财科);
                    if (indexs.Count > 0)
                    {
                        txtJiCaiKYJ.Text = Convert.ToString(drc[indexs[0]]["S_SHENPIYJ"]);
                        if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销
                        ) txtJHBGJCK.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                    }
                }

                if (laihanxx.I_LIUCHENGBH > (int) eLaiHanLCBH.处长)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int) laihanxx.I_LAIHANLX, eLaiHanLCBH.处长);
                    if (indexs.Count > 0) txtCuZhangYJ.Text = Convert.ToString(drc[indexs[0]]["S_SHENPIYJ"]);
                }
            }
        }

        /// <summary>
        ///     月度复审修改
        /// </summary>
        private void FSXGEnabled(bool enabled)
        {
            txtJHBGYWY.ClientEnabled = enabled;
            txtJHBGZR.ClientEnabled = enabled;
            txtJHBGJSK.ClientEnabled = enabled;
            txtJHBGJCK.ClientEnabled = enabled;
            txtJHBGCZ.ClientEnabled = enabled;
            cz.Visible = enabled;
            fs.Visible = enabled;
        }

        public void FSXGSave(YS_LH_LAIHANXX laihanxx)
        {
            var bllT = new YS_LH_YUETIAOZProcess();
            var dr = bllT.SelectYS_LH_YUETIAOZsDynamic("I_JLZT=0 AND S_LAIHANBH='" + laihanxx.S_LAIHANBH + "'", null)
                .Rows[0];
            var idT = Convert.ToInt32(dr["ID"]);

            var item = bllT.SelectYS_LH_YUETIAOZ(idT);
            item.I_YEWUYJY = Convert.ToInt32(txtJHBGYWY.Text);
            item.I_ZHUREN = Convert.ToInt32(txtJHBGZR.Text);
            item.I_JIESHUIKZ = Convert.ToInt32(txtJHBGJSK.Text);
            item.I_JICAIKZ = Convert.ToInt32(txtJHBGJCK.Text);
            if (!string.IsNullOrEmpty(txtJHBGCZ.Text))
                item.I_CHUZHANG = Convert.ToInt32(txtJHBGCZ.Text);
            else item.I_CHUZHANG = null;
            
            //更新复审状态
            laihanxx.I_FUSHEN = 1;
            laihanxx.I_FUSHENR = LoginInfo.CurrentUser.UserID;
            laihanxx.D_FUSHENRQ = DateTime.Now;
            if (txtFSL.Text == "0")
            {
                laihanxx.I_LIUCHENGZT = (int) eLaiHanLCZT.不通过;
                item.I_CHUZHANG = Convert.ToInt32(txtFSL.Text);
                ////取消已变更的调整
                //new SpHelper().P_YSLH_YUETIAOZFSBTG(laihanxx.S_LAIHANBH);
            }
            bllT.InsertOrUpdate(item);
            //变更的调整
            new SpHelper().P_YSLH_YUETIAOZFSBTG(laihanxx.S_LAIHANBH);

            new YS_LH_LAIHANXXProcess().InsertOrUpdate(laihanxx);
        }

        public ValidateRS FSXGSaveValidate()
        {
            var rs = new ValidateRS();
            var num = -1;
            if (!int.TryParse(txtJHBGYWY.Text, out num) || !int.TryParse(txtJHBGZR.Text, out num)
                || !int.TryParse(txtJHBGJSK.Text, out num) || !int.TryParse(txtJHBGJCK.Text, out num)
                //|| !int.TryParse(txtJHBGCZ.Text, out num)
                )
            {
                rs.IsSuccess = false;
                rs.Msg = "请输入正确的计划量";
            }
            else
            {
                rs.IsSuccess = true;
            }

            return rs;
        }

        #endregion
    }
}